package com.langsheng.lsintell.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 线程池类
 * Created by xxw on 2016/8/3.
 */
public class LSThreadPool {
    private static LSThreadPool instance = new LSThreadPool();

    public static LSThreadPool getInstance() {
        return instance;
    }

    private LSThreadPool() {
    }

    private ExecutorService cachedThreadPool;
    private ExecutorService fixedThreadPool;
    private ScheduledExecutorService scheduledThreadPool;
    private ExecutorService singleThreadExecutor;

    public void addCachedThread(Runnable runnable) {
        if (cachedThreadPool == null) {
            cachedThreadPool = Executors.newCachedThreadPool();
        }

        cachedThreadPool.execute(runnable);
    }

    public void addFixedThread(Runnable runnable) {
        if (fixedThreadPool == null) {
            fixedThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        }
        fixedThreadPool.execute(runnable);
    }

    public void addScheduleThread(Runnable runnable, long delay, long schedule) {
        if (scheduledThreadPool == null) {
            scheduledThreadPool = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
        }
        scheduledThreadPool.scheduleAtFixedRate(runnable, delay, schedule, TimeUnit.SECONDS);
    }

    public void addDelayedThread(Runnable runnable, long delay) {
        if (scheduledThreadPool == null) {
            scheduledThreadPool = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
        }
        scheduledThreadPool.schedule(runnable, delay, TimeUnit.SECONDS);
    }

    public void addSingleThread(Runnable runnable) {
        if (singleThreadExecutor == null) {
            singleThreadExecutor = Executors.newSingleThreadExecutor();
        }
        singleThreadExecutor.execute(runnable);
    }

    public void shutdownCachThread() {
        if (cachedThreadPool != null && !cachedThreadPool.isShutdown()) {
            cachedThreadPool.shutdownNow();
        }
    }

    public void shutdownFiexedThread() {
        if (fixedThreadPool != null && !fixedThreadPool.isShutdown()) {
            fixedThreadPool.shutdownNow();
        }
    }

    public void shutdownScheduleThread() {
        if (scheduledThreadPool != null && !fixedThreadPool.isShutdown()) {
            scheduledThreadPool.shutdownNow();
        }
    }

    public void shutdownSingleThread() {
        if (singleThreadExecutor != null && !singleThreadExecutor.isShutdown()) {
            singleThreadExecutor.shutdownNow();
        }
    }

    public void shutdownAll() {
        shutdownCachThread();
        shutdownFiexedThread();
        shutdownScheduleThread();
        shutdownSingleThread();
    }

    public void reset() {
        shutdownAll();
        cachedThreadPool = null;
        fixedThreadPool = null;
        scheduledThreadPool = null;
        singleThreadExecutor = null;
    }

}
